﻿Imports System.Collections.ObjectModel
Imports Licenta3.Common.Classes
Imports Microsoft.Office.Interop.Word
Imports System.IO
Imports Licenta3.Helpers
<Serializable()>
Public Class Anexa3Class
    Inherits AnexaClass

    Private _docCadastrala
    Public Property docCadastrala As String
        Get
            Return _docCadastrala
        End Get
        Set(value As String)
            'If TipDocCadastrala.values.Contains(value) Then
            _docCadastrala = value
            'End If
            OnPropertyChanged("docCadastrala")
        End Set
    End Property

    Private WithEvents _Proprietari As New ObservableCollection(Of ProprietarClass)
    Public Property Proprietari As ObservableCollection(Of ProprietarClass)
        Get
            Return _Proprietari
        End Get
        Set(value As ObservableCollection(Of ProprietarClass))
            _Proprietari = value
            OnPropertyChanged("Proprietari")
            If _Proprietari.Count > 0 Then
                Me.proprietar = _Proprietari(0)
            End If
        End Set
    End Property
    Private WithEvents _Imobile As New ObservableCollection(Of Imobil)
    Public Property Imobile As ObservableCollection(Of Imobil)
        Get
            Return _Imobile
        End Get
        Set(value As ObservableCollection(Of Imobil))
            _Imobile = value
            OnPropertyChanged("Imobile")
            If _Imobile.Count > 0 Then
                Me.Imobil = _Imobile(0)
                If _Imobile.Count = 1 Then
                    Me.docCadastrala = New TipDocCadastrala().values(TipDocCadastrala.EnumDocCadastrala.Dezlipire)
                Else
                    Me.docCadastrala = New TipDocCadastrala().values(TipDocCadastrala.EnumDocCadastrala.Alipire)
                End If
            End If
            _Imobile.First.UAT = Imobil.UAT
            _Imobile.Last.UAT = Imobil.UAT
        End Set
    End Property
    Private _RaspunsPrin As String = TipRaspuns.values.Item(2)
    Public Property RaspunsPrin As String
        Get
            Return _RaspunsPrin
        End Get
        Set(value As String)
            _RaspunsPrin = value
            OnPropertyChanged("RaspunsPrin")
        End Set
    End Property

    Public Sub New()
        Me.ShortName = "ANEXA3"
    End Sub

    Private Sub _Proprietari_CollectionChanged(sender As Object, e As System.Collections.Specialized.NotifyCollectionChangedEventArgs) Handles _Proprietari.CollectionChanged
        If e.Action = Specialized.NotifyCollectionChangedAction.Add Or e.Action = Specialized.NotifyCollectionChangedAction.Replace Then
            If e.NewItems.Count > 0 Then Me.proprietar = e.NewItems(0)
        End If
    End Sub
    Private Sub _Imobile_CollectionChanged(sender As Object, e As System.Collections.Specialized.NotifyCollectionChangedEventArgs) Handles _Imobile.CollectionChanged
        If e.Action = Specialized.NotifyCollectionChangedAction.Add Or e.Action = Specialized.NotifyCollectionChangedAction.Replace Then
            e.NewItems(0).UAT = Imobil.UAT
        End If
    End Sub

    Public Overrides Sub toWordOrPDF(Optional format As Microsoft.Office.Interop.Word.WdSaveFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault, Optional printOnly As Boolean = False)
        Dim dezlipire As Boolean = True
        'Dim i As Integer
        'Dim currentDirectory As String
        Dim Word As New Application
        Dim AnexaDoc As New Document

        Dim sablon As Object
        If Me.Proprietari.Count = 1 Then
            sablon = (assemblyPath & "\WordTemplates\" & Me.ShortName & ".dotx")
        Else
            sablon = (assemblyPath & "\WordTemplates\" & Me.ShortName & "m.dotx")
        End If
        If Me.Imobile.Count > 1 Then dezlipire = False
        AnexaDoc = Word.Documents.Add(sablon, False, WdNewDocumentType.wdNewBlankDocument, False)
        Dim currentDirectory = SetariClass._LucruPath & "\" & Me.Autorizat.Nume & "\" & Me.OCPI.denumire & "\" & Me.Imobil.UAT.Denumire & "\" & Me.proprietar.Nume
        If IO.Directory.Exists(currentDirectory) = False Then
            IO.Directory.CreateDirectory(currentDirectory)
        End If

        FillText("OCPI", AnexaDoc, Me.OCPI.denumire)
        FillText("BCPI", AnexaDoc, Me.BCPI.denumire)
        Dim oneForDezlipire As Integer = 2
        If Me.Imobile.Count = 1 Then oneForDezlipire = 1
        FillList("OneForDezlipire", AnexaDoc, oneForDezlipire, False)
        If Proprietari.Count = 1 Then
            FillText("NumeProprietar", AnexaDoc, Me.proprietar.Nume)
            Dim oneForPF As Integer = 2
            If Me.proprietar.IsPF Then oneForPF = 1
            FillList("OneForPF", AnexaDoc, oneForPF, False)
            FillText("AdresaProprietar", AnexaDoc, Me.proprietar.adresa.adresaIntreaga)
            If proprietar.IsPF = False Then DeleteCC("ProprietarIsPJ2bd", AnexaDoc, False)
            FillText("SerieCIProprietar", AnexaDoc, Me.proprietar.SerieCI)
            FillText("NrCIProprietar", AnexaDoc, Me.proprietar.NrCI)
            FillText("IDProprietar", AnexaDoc, Me.proprietar.id)
            FillText("telProprietar", AnexaDoc, Me.proprietar.tel)
            FillText("emailProprietar", AnexaDoc, proprietar.email)
        Else
            Dim tabelProprietari As Microsoft.Office.Interop.Word.Table
            tabelProprietari = AnexaDoc.Tables.Add(AnexaDoc.Bookmarks("TabelProprietari").Range, Proprietari.Count + 1, 7)
            With tabelProprietari
                .Cell(1, 1).Range.Text = "Nume"
                .Cell(1, 2).Range.Text = "Adresa"
                .Cell(1, 3).Range.Text = "Serie CI"
                .Cell(1, 4).Range.Text = "Nr. CI"
                .Cell(1, 5).Range.Text = "CNP/CUI"
                .Cell(1, 6).Range.Text = "Telefon"
                .Cell(1, 7).Range.Text = "Email"
                .ApplyStyleHeadingRows = True
                For r = 2 To .Rows.Count
                    .Cell(r, 1).Range.Text = Me.Proprietari(r - 2).Nume
                    .Cell(r, 2).Range.Text = Me.Proprietari(r - 2).adresa.adresaIntreaga
                    .Cell(r, 3).Range.Text = Me.Proprietari(r - 2).SerieCI
                    .Cell(r, 4).Range.Text = Me.Proprietari(r - 2).NrCI
                    .Cell(r, 5).Range.Text = Me.Proprietari(r - 2).id
                    .Cell(r, 6).Range.Text = Me.Proprietari(r - 2).tel
                    .Cell(r, 7).Range.Text = Me.Proprietari(r - 2).email
                Next
            End With
            tabelProprietari.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle
            tabelProprietari.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle
        End If
        FillText("NumeMandatar", AnexaDoc, Autorizat.Nume)
        'TipRaspuns.values.IndexOf(RaspunsPrin)
        'MsgBox(RaspunsPrin)
        'For i = 1 To TipRaspuns.values.Count
        '    MsgBox(TipRaspuns.values.Item(i - 1))
        'Next
        FillList("TipRaspuns", AnexaDoc, TipRaspuns.values.IndexOf(RaspunsPrin) + 1)

        Dim tabelImobile As Microsoft.Office.Interop.Word.Table
        tabelImobile = AnexaDoc.Tables.Add(AnexaDoc.Bookmarks("TabelImobile").Range, Imobile.Count + 1, 4)
        With tabelImobile
            .Cell(1, 1).Range.Text = "Adresa"
            .Cell(1, 2).Range.Text = "Nr. CF"
            .Cell(1, 3).Range.Text = "Nr. Cadastral"
            .Cell(1, 4).Range.Text = "UAT"
            .ApplyStyleHeadingRows = True
            For r = 2 To .Rows.Count
                .Cell(r, 1).Range.Text = Me.Imobile(r - 2).Adresa.adresaIntreaga
                .Cell(r, 2).Range.Text = Me.Imobile(r - 2).NrCF
                .Cell(r, 3).Range.Text = Me.Imobile(r - 2).NrCadastral
                .Cell(r, 4).Range.Text = Me.Imobile(r - 2).UAT.Denumire
            Next
        End With
        tabelImobile.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle
        tabelImobile.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle

        Dim ext As String = ".docx"
        If format = WdSaveFormat.wdFormatPDF Then ext = ".pdf"
        If printOnly = True Then
            Try
                AnexaDoc.Activate()
                If File.Exists(currentDirectory & "\" & Me.ShortName & ".docx") = False Then
                    AnexaDoc.SaveAs2(currentDirectory & "\" & Me.ShortName & ".docx")
                Else
                    File.Delete(currentDirectory & "\" & Me.ShortName & ".docx")
                    AnexaDoc.SaveAs2(currentDirectory & "\" & Me.ShortName & ".docx")
                End If
                Word.PrintOut(FileName:=currentDirectory & "\" & Me.ShortName & ".docx")

            Catch ex As Exception
                MsgBox(ex.Message + vbCrLf + ex.Source, MsgBoxStyle.Information)
            End Try
        Else
            Try
                If File.Exists(currentDirectory & "\" & Me.ShortName & "" & ext) = False Then
                    AnexaDoc.SaveAs2(currentDirectory & "\" & Me.ShortName & "" & ext, format)
                Else
                    If MsgBox("Fisierul exista! Doriti sa-l suprascrieti?", MsgBoxStyle.YesNo, "Sunteti sigur?") = MsgBoxResult.Yes Then
                        File.Delete(currentDirectory & "\" & Me.ShortName & ext)
                        AnexaDoc.SaveAs2(currentDirectory & "\" & Me.ShortName & ext, format)
                    Else
                        MsgBox("Niciun fisier nu a fost modificat", MsgBoxStyle.OkOnly)
                    End If
                End If
            Catch ex As Exception
                MsgBox(ex.Message + vbCrLf + ex.Source + vbCrLf + ex.StackTrace, vbOKOnly)
            End Try
        End If

        AnexaDoc.Close(WdSaveOptions.wdDoNotSaveChanges)
        AnexaDoc = Nothing
        'AnexaDoc.SaveAs2(currentDirectory & "\ANEXA1.docx")
        'Word.Visible = True
        Word.Quit(WdSaveOptions.wdDoNotSaveChanges)
        Word = Nothing
    End Sub


End Class
